package cn.cdut.yiwenqian.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.cdut.yiwenqian.dao.interfaces.INewInformationDao;
import cn.cdut.yiwenqian.entity.CdutPostNews;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Scope("singleton")
@Repository
public class NewInformationDaoImpl extends HibernateDaoSupport implements INewInformationDao {

	@Override
	public CdutPostNews findNewsById(Integer id) {
		return this.getHibernateTemplate().get(CdutPostNews.class, id);
	}

	@Override
	public MyPageToolVo<CdutPostNews> fingNewsByName(final String name, final int page) {
		final MyPageToolVo<CdutPostNews> rpage=new MyPageToolVo<CdutPostNews>();
		rpage.setPageNum(page);
		rpage.setList(this.getHibernateTemplate().execute(new HibernateCallback<List<CdutPostNews>>(){

			@Override
			public List<CdutPostNews> doInHibernate(Session session) throws HibernateException {
				Query query=session.createQuery("from CdutPostNews n where n.name like ?");
				query.setString(0, name);
				rpage.setPageCount(query.list().size()/15+1);
				query.setFirstResult((page-1)*15);
				query.setMaxResults(15);
				return query.list();
			}
			
		}));
		return rpage;
	}

	@Override
	public CdutPostNews saveNews(CdutPostNews info) {
		info.setBeginDate(new Date());
		this.getHibernateTemplate().save(info);
		return info;
	}

	@Override
	public void updateNews(CdutPostNews info) {
		this.getHibernateTemplate().update(info);
		
	}

	@Override
	public MyPageToolVo<CdutPostNews> fingNewsByAuthorName(final Integer authorId, final int page) {
		final MyPageToolVo<CdutPostNews> rpage=new MyPageToolVo<CdutPostNews>();
		rpage.setPageNum(page);
		rpage.setList(this.getHibernateTemplate().execute(new HibernateCallback<List<CdutPostNews>>(){

			@Override
			public List<CdutPostNews> doInHibernate(Session session) throws HibernateException {
				Query query=session.createQuery("from CdutPostNews n where n.cdutPostUsers.unumber = ?");
				query.setInteger(0, authorId);
				rpage.setPageCount(query.list().size()/15+1);
				query.setFirstResult((page-1)*15);
				query.setMaxResults(15);
				return query.list();
			}
			
		}));
		return rpage;
	}

	@Override
	public MyPageToolVo<CdutPostNews> findAllNewsInSystem(final int page) {
		final MyPageToolVo<CdutPostNews> rpage=new MyPageToolVo<CdutPostNews>();
		rpage.setPageNum(page);
		rpage.setList(this.getHibernateTemplate().execute(new HibernateCallback<List<CdutPostNews>>(){

			@Override
			public List<CdutPostNews> doInHibernate(Session session) throws HibernateException {
				Query query=session.createQuery("from CdutPostNews n ");
				rpage.setPageCount(query.list().size()/15+1);
				query.setFirstResult((page-1)*15);
				query.setMaxResults(15);
				return query.list();
			}
			
		}));
		return rpage;
	}
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

}
